package bancoperu.rest;


import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;
import bancoperu.dao.CustomerDAO;
import bancoperu.model.Customer;
import bancoperu.util.Converter;
import bancoperu.model.Security;
import bancoperu.dao.SecurityDAO;

@Path("/customer")
public class CustomerRest {

	@GET
	@Path("/retrieve")
	@Produces(MediaType.TEXT_PLAIN)
	public String retrieveCustomer( @QueryParam("customerID") String customerID 
									) {		
		
		JSONObject objeto = new JSONObject();
		
		try {
			CustomerDAO dao = new CustomerDAO();
			Customer vo = dao.retrieveCustomer(customerID);		
			
			objeto.put("CustomerID", vo.getCustomerId());
			objeto.put("CustomerPassword", vo.getCustomerPassword());
			objeto.put("PasswordDate", Converter.utilDateToString(vo.getPasswordDate()));
			objeto.put("LoginAttempts", vo.getLoginAttempts());
			
			
			
		} catch (Exception e) {	
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return objeto.toString();
	}
	
	
		
	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.TEXT_PLAIN)
	public String createCustomer( @FormParam("customerID") String customerID,
								  			     @FormParam("customerPassword") String customerPassword ) {		
		
		System.out.println(customerID);
		System.out.println(customerPassword);
		JSONObject objeto = new JSONObject();
		
		try {
			
			CustomerDAO dao = new CustomerDAO();
			Customer vo = new Customer();
			vo.setCustomerId(customerID);
			vo.setCustomerPassword(customerPassword);
			vo.setPasswordDate(new Date());
			vo.setLoginAttempts(0);
			dao.insert(vo);
			
			objeto.put("status", "OK");
			
		} catch (Exception e) {	
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return objeto.toString();
	}
	
	@POST
	@Path("/login")
	@Produces(MediaType.TEXT_PLAIN)
	public String loginCustomer( @FormParam("customerID") String customerID ,
								 @FormParam("customerPassword") String customerPassword
									) {		
		
		System.out.println(customerID);
		System.out.println(customerPassword);
		
		JSONObject objeto = new JSONObject();
		
		try {
			CustomerDAO dao = new CustomerDAO();
			Customer vo = dao.checkcustomer(customerID, customerPassword);		
			
			objeto.put("CustomerID", vo.getCustomerId());
			objeto.put("CustomerPassword", vo.getCustomerPassword());
			//objeto.put("PasswordDate", Converter.utilDateToString(vo.getPasswordDate()));
			//objeto.put("LoginAttempts", vo.getLoginAttempts());
			
			objeto.put("status", "valid" );
			
			
			
		} catch (Exception e) {			
			System.out.println(e.getMessage());
			
			objeto.put("MSG", e.getMessage());
		}
		return objeto.toString();
	}
	
	
	
	@POST
	@Path("/security")
	@Produces(MediaType.TEXT_PLAIN)
	public String checkAnswer( @FormParam("customerID") String customerID,
							   @FormParam("Question") String Question,
							   @FormParam("Answer") String Answer){		
		
		
		JSONObject objeto = new JSONObject();
		
		try {
			SecurityDAO dao = new SecurityDAO();
			Customer vo = new Customer();
			vo.setCustomerId(customerID);
			boolean b = false;
			boolean va = dao.checkQuestion(customerID, Question, Answer);		
			System.out.print(va);
			if(b == va)
			{
				CustomerDAO dao2 = new CustomerDAO();
				dao2.updateattempts(vo);
				
				objeto.put("status", "false");
				
			}
			
			else{
				objeto.put("status", "true");
			}
			
		} catch (Exception e) {			
			System.out.println(e.getMessage());
			
			objeto.put("MSG", e.getMessage());
		}
		return objeto.toString();
	}
	
	@POST
	@Path("/updatePassword")
	@Produces(MediaType.TEXT_PLAIN)
	public String updatePassword( @FormParam("customerID") String customerID ,
								  @FormParam("customerPassword") String customerPassword,
								  @FormParam("customerPassword") String newPassword
									) {		
		
		
		JSONObject objeto = new JSONObject();
		
		try {
			CustomerDAO dao = new CustomerDAO();
			dao.updatePassword(customerID, customerPassword, newPassword);		
			
			
			objeto.put("status", "valid" );
			
			
			
		} catch (Exception e) {			
			System.out.println(e.getMessage());
			
			objeto.put("MSG", e.getMessage());
		}
		return objeto.toString();
	}
	
}
